package com.sp2p.util;

import java.security.Key; 
import java.security.SecureRandom; 
import java.security.spec.AlgorithmParameterSpec; 
import javax.crypto.Cipher; 
import javax.crypto.SecretKeyFactory; 
import javax.crypto.spec.DESKeySpec; 
import javax.crypto.spec.IvParameterSpec; 

import org.apache.axis.encoding.Base64;

public class DataEncode {
	public static final String ALGORITHM_DES = "DES/CBC/PKCS5Padding";
	
	//加密算法
	public static String encode(String key,byte[] data) throws Exception{
		DESKeySpec dks = new DESKeySpec(key.getBytes());
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		//key的长度不能够小于8位字节
		Key secretKey = keyFactory.generateSecret(dks);
		Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
		IvParameterSpec iv=new IvParameterSpec("12345678".getBytes());//向量
		AlgorithmParameterSpec paramSpec = iv;
		
		cipher.init(Cipher.ENCRYPT_MODE, secretKey,paramSpec);
		byte[] bytes = cipher.doFinal(data);
		System.out.println(new String(bytes));
		return Base64.encode(bytes);
	}
	
	//解密算法
	public static byte[] decode(String key,byte[] data) throws Exception{
		try{
			SecureRandom sr = new SecureRandom(); 
			DESKeySpec dks = new DESKeySpec(key.getBytes()); 
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); 
			//key的长度不能够小于8位字节 
			Key secretKey = keyFactory.generateSecret(dks); 
			Cipher cipher = Cipher.getInstance(ALGORITHM_DES); 
			IvParameterSpec iv = new IvParameterSpec("12345678".getBytes()); 
			AlgorithmParameterSpec paramSpec = iv; 
			cipher.init(Cipher.DECRYPT_MODE, secretKey,paramSpec); 
			return cipher.doFinal(data); 

		} catch (Exception e){
			e.printStackTrace();
			throw new Exception(e);
		}
		}
	
	
	/**将二进制转换成16进制 
	 * @param buf 
	 * @return 
	 */  
	public static String parseByte2HexStr(byte buf[]) {  
	        StringBuffer sb = new StringBuffer();  
	        for (int i = 0; i < buf.length; i++) {  
	                String hex = Integer.toHexString(buf[i] & 0xFF);  
	                if (hex.length() == 1) {  
	                        hex = '0' + hex;  
	               }  
	                sb.append(hex.toUpperCase());  
	        }  
	        return sb.toString();  
	} 

	/**将16进制转换为二进制 
	 * @param hexStr 
	 * @return 
	 */  
	public static byte[] parseHexStr2Byte(String hexStr) {  
	        if (hexStr.length() < 1)  
	                return null;  
	        byte[] result = new byte[hexStr.length()/2];  
	        for (int i = 0;i< hexStr.length()/2; i++) {  
	                int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);  
	                int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);  
	                result[i] = (byte) (high * 16 + low);  
	        }  
	        return result;  
	} 

	
	   /** 
      * 获取编码后的值 
      * @param key 
      * @param data 
      * @return 
      * @throws Exception  
      * @throws Exception 
      */ 
	   public static String decodeValue(String key,String data) throws Exception  
	     { 
         byte[] datas; 
         String value = null; 
         datas = decode(key, Base64.decode(data)); 
         value = new String(datas); 
        if (value.equals("")){ 
	             throw new Exception(); 
	         } 
	         return value; 
	     } 

	
	public static void main(String[] args) throws Exception {
		//加密算法
		String code = DataEncode.encode("12345678", "abc".getBytes());
		String encryptResultStr = parseByte2HexStr(code.getBytes()); 
		System.out.println(encryptResultStr);
		
		
		String value = DataEncode.decodeValue("12345678", code);
		System.out.println(value);
		
		//解密算法
		byte[] bytes = DataEncode.decode("12345678", encryptResultStr.getBytes());
		System.out.println(new String(bytes));
	}
}

	